<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //为什么要拷贝？ 主要原因引用类型按址赋值，会受影响，为了不被修改，需开辟新空间。

      //数组
      //     var hobby1 = ['踢毽子','看美女']
      //    var hobby2 = hobby1
      //    console.log(hobby1)
      //    console.log( hobby2)
      //    console.log( hobby2 === hobby1) //按址赋值

      //数组拷贝
      var hobby1 = ['踢毽子', '看美女'];
    //   var hobby2 = []; //开辟新数组
    //   //遍历原数组
    //   for (var i = 0; i < hobby1.length; i++)  {
    //     //放入新数组
    //     hobby2.push(hobby1[i]);
    //   }
    //   console.log(hobby2);
    //   console.log(hobby1);
    //   console.log(hobby2 === hobby1); //false ,创建的不等于原数组，互不影响

      //封装数组拷贝函数
      function deepCopy(arr){
            var result = [] 
        for(var i = 0 ; i < arr.length ; i++){
            result.push(hobby1[i]);
        }
        return result ;
      }
       var hobby2 = deepCopy(hobby1)
      var hobby1 = ['踢毽子', '看美女'];
      console.log( hobby1)
      console.log(hobby2)
    </script>
  </body>
</html>
